import React, { Component } from 'react';
import { adminRoutes } from './routes';
import { Route, Switch, Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import { Frame } from './components';
const menus = adminRoutes.filter(route => route.isNav);
const mapState = state => {
  return {
    isLogin: state.User.isLogin,
    role: state.User.role
  };
};
@connect(mapState)
class App extends Component {
  render() {
    return this.props.isLogin ? (
      //登录状态失效，重新登录
      <Frame menus={menus}>
        <Switch>
          {adminRoutes.map(route => {
            return (
              <Route
                exact={route.exact}
                key={route.pathname}
                path={route.pathname}
                render={routeProps => {
                  return route.roles.includes(this.props.role) ? (
                    <route.component {...routeProps} />
                  ) : (
                    <Redirect to="/admin/noauth" />
                  );
                }}
              ></Route>
            );
          })}
          <Redirect to={adminRoutes[0].pathname} from="/admin" exact />
          <Redirect to="/404" /> {/*一定要添加这个无法匹配的路由 */}
        </Switch>
      </Frame>
    ) : (
      <Redirect to="/login"></Redirect>
    );
  }
}
export default App;
